package com.ext.demoredis.service;

import com.ext.demoredis.common.DistributedLock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class LockService {

    private final static String LOCK_ID = "happyjava";
    @Autowired
    DistributedLock distributedLock;
    @Scheduled(cron = "0/10 * * * * ? ")
    public void doSomething() {
        String value = "good";
        boolean lock = distributedLock.getLock(LOCK_ID, value,10 * 1000);
        if (lock) {
            System.out.println("执行任务");
            distributedLock.releaseLock(LOCK_ID);
        } else {
            System.out.println("没有抢到锁");
        }
    }
}
